package com.mongo.test;

import com.mongo.MongoDBApplication;
import com.tanhua.mongo.Places;
import lombok.NonNull;
import org.bson.types.ObjectId;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.geo.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.index.GeoSpatialIndexType;
import org.springframework.data.mongodb.core.index.GeospatialIndex;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.NearQuery;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MongoDBApplication.class)
public class PlacesTest01 {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Test
    public void testSave(){
        Places p1 = new Places();
        p1.setId(ObjectId.get());
        p1.setAddress("江苏省宿迁市沭阳县南湖街道");
        p1.setTitle("县政府");
        p1.setLocation(new GeoJsonPoint(118.811129,34.117527));
        mongoTemplate.save(p1);


        Places p2 = new Places();
        p2.setId(ObjectId.get());
        p2.setAddress("江苏省宿迁市沭阳县南湖街道");
        p2.setTitle("万达广场");
        p2.setLocation(new GeoJsonPoint(118.770876,34.101216));
        mongoTemplate.save(p2);


        Places p3 = new Places();
        p3.setId(ObjectId.get());
        p3.setAddress("江苏省宿迁市沭阳县南湖街道");
        p3.setTitle("百蒙物流园");
        p3.setLocation(new GeoJsonPoint(118.76609,34.05545));
        mongoTemplate.save(p3);

        Places p4 = new Places();
        p4.setId(ObjectId.get());
        p4.setAddress("江苏省宿迁市沭阳县南湖街道");
        p4.setTitle("大唐世家");
        p4.setLocation(new GeoJsonPoint(118.772932,34.085451));
        mongoTemplate.save(p4);


    }


    @Test
    public  void testNear(){
        //1 给定坐标，相当于圆心
        GeoJsonPoint point = new GeoJsonPoint(118.763871, 34.072904);
        //2 构造半径
        Distance distance = new Distance(50, Metrics.KILOMETERS);
        //3 画圆
        Circle circle = new Circle(point, distance);
        //4 构造query对象
        Query query = new Query(Criteria.where("location").withinSphere(circle));
        //5 查询
        List<Places> places = mongoTemplate.find(query, Places.class);
        places.forEach(System.out::println);
    }

    //手动创建索引
    @Test
    public  void createIndex(){
        mongoTemplate.indexOps(Places.class).ensureIndex(new GeospatialIndex("location").typed(GeoSpatialIndexType.GEO_2DSPHERE));
    }

    @Test
    public void testNearAndDistance(){
        GeoJsonPoint point = new GeoJsonPoint(118.763871, 34.072904);
        //构造NearQuery对象
        NearQuery nearQuery = NearQuery.near(point, Metrics.KILOMETERS).maxDistance(5, Metrics.KILOMETERS);
        //查询
        GeoResults<Places> geoResults = mongoTemplate.geoNear(nearQuery, Places.class);
        //打印输出
        for (GeoResult<Places> gr:geoResults){
            @NonNull Places p = gr.getContent();
            System.out.print(p.getAddress()+":"+p.getTitle()+":");
            System.out.println(gr.getDistance());
        }

    }

}
